package com.wellview.template;
import android.app.Application;
import android.content.Context;
import android.os.StrictMode;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.register.HuaWeiRegister;
import com.mob.MobApplication;
import com.mob.MobSDK;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsListener;
import com.wellview.template.logger.Logger;
import com.wellview.template.modle.BaseBehavior;
import com.wellview.template.presenter.BaseProvider;
import com.wellview.template.utils.ScreenUtil;
import com.wellview.template.utils.XulUtils;


/**
 * Created by jizi.chen on 2017/8/28.
 */

public class App extends Application {

    private static App application;
    private static final String TAG = "App";
    private static boolean isX5Core;
    private static boolean isPushInitSuccess = false;

    @Override
    public void onCreate() {
        Logger.i(TAG,"time : 1"+ XulUtils.timestamp());
        super.onCreate();
        application = this;
        BaseProvider.init();
        BaseBehavior.init();
        MobSDK.init(this);
        initX5();
//        initTbs();
        initCloudChannel(application);
        extraHandle();


    }

    private void extraHandle() {
        //以这种方式，VM会忽略文件URI曝光。
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();

    }

    public static void initX5() {
        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        QbSdk.preInit(getInstance(), new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                Logger.i(TAG,"X5内核 预加载初始化完成");
            }
            @Override
            public void onViewInitFinished(boolean b) {
                Logger.i(TAG,"X5内核 View预加载完成 "+b);
            }
        });
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
            @Override
            public void onViewInitFinished(boolean arg0) {
                Logger.i(TAG,"X5内核初始化完成");
            }
            @Override
            public void onCoreInitFinished() {
                Logger.i(TAG,"X5内核初始化完成");
            }
        };

        QbSdk.setTbsListener(new TbsListener() {
            @Override
            public void onDownloadFinish(int i) {
                Logger.i(TAG,"腾讯X5内核 下载结束");
            }

            @Override
            public void onInstallFinish(int i) {
                Logger.i(TAG,"腾讯X5内核 安装完成");
            }

            @Override
            public void onDownloadProgress(int i) {
                Logger.i(TAG,"腾讯X5内核 下载进度:%" + i);
            }
        });
        QbSdk.initX5Environment(getInstance(), cb);
        QbSdk.setDownloadWithoutWifi(true);
    }



    /**
     * 获取APP单例
     */
    public static App getInstance() {
        if (application == null) {
            Logger.e(TAG, "getInstance and app is null");
        }
        return application;
    }


    public static boolean isX5Core() {
        return isX5Core;
    }



    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        final CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Logger.d(TAG, "init cloudchannel success"+response);
                isPushInitSuccess = true;
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Logger.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
                isPushInitSuccess = false;
            }
        });

        HuaWeiRegister.register(applicationContext);
    }

    /**
     * 阿里云推送服务
     * @return 阿里云推送服务
     */
    public CloudPushService getCloudPushService(){
        return PushServiceFactory.getCloudPushService();
    }

}
